<?php
include APPLICATION_PATH . "/models/Report2.php";
/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
class Admin_Report2Controller extends Zend_Controller_Action{
    public function init(){       
        $bootstrap = $this->getInvokeArg("bootstrap");
        $this->aConfig = $bootstrap->getOptions();
        $this->view->aConfig = $this->aConfig;
        //$this->modelMapper= new Model_ProvinceMapper();
        $this->model= new Model_Report2();
    }
    public function indexAction(){
       $this->view->controller = $this;
        $this->view->item=$this->model->fetchAllReport();
        //Zend_Debug::dump($this->model->fetchAllReport());
    }
    public function serviceAction(){
        $this->_helper->layout->disableLayout();
        $menber   = array();
        foreach ($this->model->fetchAllReport() as $items) {
            //$entry = new Model_Province();
            array_push($menber, $items);
            //$entries[] = $entry;
        }        
        echo json_encode($menber);        
        exit();
    }
    public function exportAction(){
        $this->_helper->layout->disableLayout();
        if($this->getRequest()->isPost()){
            $objPHPExcel = new PHPExcel();
            $objPHPExcel->setActiveSheetIndex(0);
            $rowCount = 4;
            $stt=1;
            $objPHPExcel->getActiveSheet()->setCellValue('A3', "STT");
            $objPHPExcel->getActiveSheet()->setCellValue('B3', "Tên Đại Lý/CH");
            $objPHPExcel->getActiveSheet()->setCellValue('C3', "Địa Chỉ");
            $objPHPExcel->getActiveSheet()->setCellValue('D3', "Huyện/TX/TP");
            $objPHPExcel->getActiveSheet()->setCellValue('E3', "Tỉnh");
            $objPHPExcel->getActiveSheet()->setCellValue('F3', "Số ĐT");
            $objPHPExcel->getActiveSheet()->setCellValue('G3', "Người Đại Diện");
            $objPHPExcel->getActiveSheet()->setCellValue('H3', "Năm Sinh");
            $objPHPExcel->getActiveSheet()->setCellValue('I3', "MST");
            $objPHPExcel->getActiveSheet()->setCellValue('J3', "Số CMND");
            $objPHPExcel->getActiveSheet()->setCellValue('K3', "Ghi Chú");
            foreach ($this->model->fetchAllReport() as $items) {
                
                $t_day = substr($items['birthday'], 0, 2);
                $t_month = substr($items['birthday'], 3, 2);
                $t_year = substr($items['birthday'], 6,4 );
                 // Fixed problems with offsets
                $t_date = PHPExcel_Shared_Date::FormattedPHPToExcel($t_year, $t_month, $t_day);
                $objPHPExcel->getActiveSheet()->setCellValue('A' . $rowCount, $stt);
                $objPHPExcel->getActiveSheet()->setCellValue('B' . $rowCount, $items['name']);
                $objPHPExcel->getActiveSheet()->setCellValue('C' . $rowCount, $items['address']);
                $objPHPExcel->getActiveSheet()->setCellValue('D' . $rowCount, $items['district_name']);
                $objPHPExcel->getActiveSheet()->setCellValue('E' . $rowCount, $items['province_name']);
                $objPHPExcel->getActiveSheet()->setCellValueExplicit('F' . $rowCount, $items['phone'],PHPExcel_Cell_DataType::TYPE_STRING);
                $objPHPExcel->getActiveSheet()->setCellValue('G' . $rowCount, $items['representavie']);
                if($items['birthday']=='')
                {
                $objPHPExcel->getActiveSheet()->setCellValue('H' . $rowCount, "");
                }
                else
                {
                    $objPHPExcel->getActiveSheet()->setCellValue('H' . $rowCount, $t_date);                    
                }
                $objPHPExcel->getActiveSheet()->setCellValueExplicit('I' . $rowCount, $items['tax_code'],PHPExcel_Cell_DataType::TYPE_STRING);
                $objPHPExcel->getActiveSheet()->setCellValueExplicit('J' . $rowCount, $items['cmnd'],PHPExcel_Cell_DataType::TYPE_STRING);
//                $objPHPExcel->getActiveSheet()->setCellValue('K' . $rowCount, $items->note);
                $rowCount++;
                $stt++;
            }
            $objPHPExcel->getActiveSheet()->mergeCells('A1:K2');
            $objPHPExcel->getActiveSheet()->setCellValue('A1', "Báo Cáo Đại Lý Cấp 2/Cửa Hàng");
            $style_alignment = array('horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
                'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle("A3:K" . $rowCount)->getAlignment()->applyFromArray($style_alignment);
            $styleArray = array(
                'borders' => array(
                    'allborders' => array(
                        'style' => PHPExcel_Style_Border::BORDER_THIN,
                    ),
                ),
            );
            $styledataArray = array(
                'font' => array(
                    'bold' => true,
                    'color' => array('rgb' => '080808'),
                    'size' => 15,
                    'name' => 'Times New Roman'
            ));
            $styletitleArray=array(
                'font' => array(
                    'bold' => true,
                    'color' => array('rgb' => '080808'),
                    'size' => 10,
                    'name' => 'Times New Roman')
            );
            $objPHPExcel->getActiveSheet()->getStyle('H4:H'. ($rowCount - 1))->getNumberFormat()->setFormatCode("dd-mm-yyyy");
            $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->applyFromArray($style_alignment);
            $objPHPExcel->getActiveSheet()->getStyle('A1')->applyFromArray($styledataArray);
            $objPHPExcel->getActiveSheet()->getStyle('A3:K3')->applyFromArray($styletitleArray);
            $objPHPExcel->getActiveSheet()->getStyle('A3:K' . ($rowCount - 1))->applyFromArray($styleArray);
            foreach (range('A', 'K') as $columnID) {
                $objPHPExcel->getActiveSheet()->getColumnDimension($columnID)->setAutoSize(true);
            }
            $filename='BaoCaoDaiLyCap2-Thang '.date('m-Y').'.xls';
            header('Content-Type: application/vnd.ms-excel');
            header('Content-Disposition: attachment;filename='.$filename);
            header('Cache-Control: max-age=0');
            $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
            $objWriter->save('php://output');
            exit();
        }
    }
}
